

import React, { useState, useEffect, useRef } from 'react'
import "../../../style/commodity.scss"
import { Form, Input, Button, Radio, notification, Modal } from 'antd';
import { InfoCircleOutlined, } from '@ant-design/icons';
import { ajax } from '../../../api/index';


const { TextArea } = Input

const AddCommodity = () => {
  const test: any = useRef()
  const information = JSON.parse(localStorage.getItem("DeskUser") || "")
  
  const Addhomehot = async (values: any,msg:String) => {
    const res = await ajax.Addhomehot(values)
    if (res.msg == "添加成功") {
      Modal.success({
        content: res.msg + msg,
      });
      
    } else {
      Modal.success({
        content: res.msg,
      });
    }
  }


  const onFinish = (values: any) => {

    values = {
      name: values.name,
      updateTime: new Date().toLocaleDateString(),
      principal: information.name,
      img: values.imgurl,
      color: values.color,
      size: values.size,
      discountPrice: values.discountPrice,      //折扣价格
      sellCount: values.sellCount,     //月销总数
      originalPrice: values.originalPrice,
      inventory: values.inventory   //商品库存
    }
    Addhomehot(values,"共 1 条，已添加 1 条")
  };

  const onFinishFailed = (errorInfo: any) => {
    console.log('Failed:', errorInfo);
  };

  const onForm = () => {
    let jsonS = JSON.parse(test.current.resizableTextArea.textArea.value)
    let a = 0
    const jsq = setInterval(() => {
      let jsonSS = {
        name: jsonS[a].goodsName,
        updateTime: jsonS[a].updateTime,
        principal: information.name,
        img: "https://cdn.bestseller.com.cn" + jsonS[a].gscMaincolPath.replace(/_p3.jpg/, "_p7.jpg"),
        color: jsonS[a].colorAlias,
        size: "XS,S,M,L,XL",
        discountPrice: jsonS[a].discountPrice,      //折扣价格
        sellCount: jsonS[a].sellCount,     //月销总数
        originalPrice: jsonS[a].originalPrice,
        inventory: jsonS[a].id,  //商品库存
        categoryName: jsonS[a].categoryName,
        gsColorCode: jsonS[a].gsColorCode , //编号
        key:jsonS[a].id 
      }
      Addhomehot(jsonSS,`共 ${jsonS.length} 条，已添加 ${a+1} 条`)
      a++
      console.log(jsonSS);
      
      if(a == jsonS.length){
        clearInterval(jsq)
        test.current.resizableTextArea.textArea.value = ""
      }
    }, 400)


    // Addhomehot(jsonS)
  }
  return (
    <div className='List'>
      <TextArea rows={4} ref={test} />
      <Button type="primary" htmlType="submit" onClick={onForm} style={{ margin: "20px 0 0 45%" }}>
        加入商品库存
      </Button>
      <Form
        name="basic"
        labelCol={{ span: 24 }}
        wrapperCol={{ span: 24 }}
        // initialValues={{ remember: true }}
        onFinish={onFinish}
        onFinishFailed={onFinishFailed}
        autoComplete="off"
      >
        <Form.Item
          label="商品名称"
          name="name"
          rules={[{ required: true, message: '请填写商品图ImgUrl' }]}
        >
          <Input />
        </Form.Item>

        <Form.Item
          label="商品图ImgUrl"
          name="imgurl"
          rules={[{ required: true, message: '请填写商品图ImgUrl' }]}
        >
          <Input />
        </Form.Item>

        <Form.Item
          label="商品颜色"
          name="color"
          rules={[{ required: true, message: '请填写商品颜色' }]}
        >
          <Input />
        </Form.Item>

        <Form.Item
          label="商品尺寸"
          name="size"
          rules={[{ required: true, message: '请填写商品尺寸' }]}
        >
          <Input />
        </Form.Item>

        <Form.Item
          label="商品原价"
          name="originalPrice"
          rules={[{ required: true, message: '请填写商品原价' }]}
        >
          <Input />
        </Form.Item>
        <Form.Item
          label="商品折扣价"
          name="discountPrice"
          rules={[{ required: true, message: '请填写商品折扣价' }]}
        >
          <Input />
        </Form.Item>

        <Form.Item
          label="商品月销总数"
          name="sellCount"
          rules={[{ required: true, message: '请填写商品月销总数' }]}
        >
          <Input />
        </Form.Item>

        <Form.Item
          label="商品库存"
          name="inventory"
          rules={[{ required: true, message: '请填写商品库存' }]}
        >
          <Input />
        </Form.Item>

        <Form.Item wrapperCol={{ offset: 11, span: 12 }}>
          <Button type="primary" htmlType="submit">
            加入商品库存
          </Button>
        </Form.Item>
      </Form>
    </div>
  )
}

export default AddCommodity